import type { RouteRecordRaw } from 'vue-router'

const modules = import.meta.glob('./modules/*.ts', { eager: true }) as Record<string, { default: RouteRecordRaw }>
const routers: RouteRecordRaw[] = []

/**
 * 权限路由排序
 * @param routes - 权限路由
 */
function sortRoutes(routes: RouteRecordRaw[]) {
  return routes.sort((next, pre) => Number(next.meta?.order) - Number(pre.meta?.order))
}

Object.keys(modules).forEach((key) => {
  const module = modules[key].default
  if (!module) return

  if (module) {
    routers.push(module)
  } else {
    window.console.error(`路由模块解析出错: key = ${key}`)
  }
  sortRoutes(routers)
})
export default routers
